<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Nset extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    public function programlink(){
        $customer=Db::name('customer')->where('id',session('customer')['id'])->find();
        $this->assign('customer',$customer);
        return $this->fetch();

    }
    //主题色
    public function themecolor(){
        if(request()->isGet()){
            $data=Db::name('customer')->where('id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['backcolor']=$param['backcolor'];
            $data['themecolor']=$param['themecolor'];
            $data['themefont']=$param['themefont'];
            $data['bottomcolor']=$param['bottomcolor'];
            $data['uptime']=time();
            $res= Db::name('customer')->where(array('id'=>session('customer')['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    //模块设置
    public function module_set(){
        $list=Db::name('nmoduleset')->where('cus_id',session('customer')['id'])->order('id asc')->paginate(100);
        $lists=Db::name('nmoduleset')->where('cus_id',session('customer')['id'])->select();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    //模块设置--禁用
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('nmoduleset')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //模块设置--开启
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('nmoduleset')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    //关于我们
    public function aboutus(){
        if(request()->isGet()){
            $data=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['name']=$param['name'];
            $data['logo']=$param['logo'];
            $data['telephone']=$param['telephone'];
            $data['qq']=$param['qq'];
            $data['record']=$param['record'];
            $data['ico']=$param['ico'];
            $data['miit']=$param['miit'];
            $data['psr']=$param['psr'];
            $data['attr1']=$param['attr1'];
            $data['attr2']=$param['attr2'];
            $data['attr3']=$param['attr3'];
            $data['attr4']=$param['attr4'];
            $data['attrimg11']=$param['attrimg11'];
            $data['attrimg21']=$param['attrimg21'];
            $data['attrimg31']=$param['attrimg31'];
            $data['attrimg41']=$param['attrimg41'];
            $data['attrimg12']=$param['attrimg12'];
            $data['attrimg22']=$param['attrimg22'];
            $data['attrimg32']=$param['attrimg32'];
            $data['attrimg42']=$param['attrimg42'];
            $data['update_time']=time();
            $res= Db::name('naboutus')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $resin= Db::name('naboutus')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    //小程序设置
    public function wechat_set(){
        if(request()->isGet()){
            $data=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['appid']=$param['appid'];
            $data['appsecret']=$param['appsecret'];
            $data['bdappid']=$param['bdappid'];
            $data['bdappkey']=$param['bdappkey'];
            $data['bdappsecret']=$param['bdappsecret'];
            $res= Db::name('naboutus')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                $cus=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
                if($cus){
                    return json(['code' => 500,'msg' => '失败']);
                }else{
                    $resin= Db::name('naboutus')->insert($data);
                    if($resin){
                        return json(['code' => 200,'msg' => '保存成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }
            }
        }
    }
    public function tmdedit(){//修改
        $cus_id=session('customer')['id'];
        $siteroot='siteroot'."':'".config('tmdhttp').$_SERVER['HTTP_HOST'];
        $cus=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
        if ($cus['appid']) {
            $appid = file_get_contents("./download/project.config.json");
            $appidjson = str_replace(explode(':',explode(',', $appid)[5])[1],'"'.$cus['appid'].'"', $appid);
            file_put_contents("./download/project.config.json", $appidjson);
            $cusid = file_get_contents("./download/siteinfo.js");
            $update_cusid = str_replace(explode(':',explode(',', $cusid)[0])[1],"'".$cus_id."'", $cusid);
            file_put_contents("./download/siteinfo.js", $update_cusid);
            $site = file_get_contents("./download/siteinfo.js");
            $update_site = str_replace(explode(',', $site)[1],"'".$siteroot."'", $site);
            // $update_site = str_replace(explode(':',explode(',', $site)[1])[1],"'".$siteroot."'", $site);
            file_put_contents("./download/siteinfo.js", $update_site);
            $tmdzip=$this->tmdzip();
            return json(['code' => 200,'data'=>$tmdzip,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => 'appid']);
        }
    }
    private function tmdzip(){//覆盖
        $origin_str = file_get_contents("./download/siteinfo.js");
        $appidjsons = file_get_contents("./download/project.config.json");
        $zip=new \ZipArchive;
        $res=$zip->open('./download/nmp-weixin.zip',\ZipArchive::CREATE);
        if($res===TRUE){
            $zip->addFromString('nmp-weixin/siteinfo.js',$origin_str);
            $zip->addFromString('nmp-weixin/project.config.json',$appidjsons);
            $zip->close();
            return 200;
        }else {
            return 500;
        }
    }
    public function tmdeditbd(){//修改
        $cus_id=session('customer')['id'];
        $siteroot='siteroot'."':'".config('tmdhttp').$_SERVER['HTTP_HOST'];
        $cus=Db::name('naboutus')->where('cus_id',session('customer')['id'])->find();
        if ($cus['bdappid']) {
            $appid = file_get_contents("./download/project.swan.json");
            $appidjson = str_replace(explode(':',explode(',', $appid)[1])[1],'"'.$cus['bdappid'].'"', $appid);
            file_put_contents("./download/project.swan.json", $appidjson);
            $cusid = file_get_contents("./download/siteinfo.js");
            $update_cusid = str_replace(explode(':',explode(',', $cusid)[0])[1],"'".$cus_id."'", $cusid);
            file_put_contents("./download/siteinfo.js", $update_cusid);
            $site = file_get_contents("./download/siteinfo.js");
            $update_site = str_replace(explode(',', $site)[1],"'".$siteroot."'", $site);
            file_put_contents("./download/siteinfo.js", $update_site);
            $tmdzip=$this->tmdzipbd();
            return json(['code' => 200,'data'=>$tmdzip,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => 'appid']);
        }
    }
    private function tmdzipbd(){//覆盖
        $origin_str = file_get_contents("./download/siteinfo.js");
        $appidjsons = file_get_contents("./download/project.swan.json");
        $zip=new \ZipArchive;
        $res=$zip->open('./download/nmp-baidu.zip',\ZipArchive::CREATE);
        if($res===TRUE){
            $zip->addFromString('nmp-baidu/siteinfo.js',$origin_str);
            $zip->addFromString('nmp-baidu/project.swan.json',$appidjsons);
            $zip->close();
            return 200;
        }else {
            return 500;
        }
    }
}
